<template>
    <div class="login-page">'
        <el-card class="el-card">
            <template #header>振涛面经管理后台</template>
            <el-form ref="form" :model="ruleForm" :rules="rules" label-position="top" label-width="80px">
                <el-form-item label="用户名"  prop="username">
                    <el-input placeholder="请输入用户名" v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" placeholder="请输入密码" v-model="ruleForm.password"></el-input>
                </el-form-item>
                <el-form-item class="tc">
                    <el-button @click="login" type="primary">登录</el-button>
                    <el-button>重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import axios from 'axios'
import { mapMutations } from 'vuex'
export default {
  data () {
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: ['change', 'blur'] },
          { min: 5, max: 20, message: '长度在 5 到 20个字符', trigger: ['change', 'blur'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['change', 'blur'] },
          { pattern: /^\w{6,11}$/, message: '6到11位的字母数组下划线组成', trigger: ['blur', 'change'] }
        ]
      }

    }
  },
  methods: {
    ...mapMutations('user', ['updateUser']),
    // 登录
    login () {
      try {
        // 表单校验
        this.$refs.form.validate(async (res) => {
          if (!res) {
            return alert('校验不通过')
          }
          // 登录
          const { data: users } = await axios.get('http://localhost:3000/login')
          // 根据用户名和密码查找是否存在当前用户
          const user = users.find(item => item.username === this.ruleForm.username && item.password === this.ruleForm.password)
          if (user) {
            alert('登录成功')
            // this.$store.commit('user/updateUser', user)
            // 存储token到vuex中
            this.updateUser(user)
            // 存储用户到本地
            localStorage.setItem('userInfo', JSON.stringify(user))
            // 跳转到首页
            this.$router.push('/')
          } else {
            alert('用户名或者密码错误')
          }
        })
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
    display: flex;
    min-height: 100vh;
    justify-content: space-between;
    align-items: center;
    background: url('../../assets/imgs/bg.svg') no-repeat center / cover;
    overflow: hidden;

}

.el-card {
    width: 420px;
    margin: 0 auto;

    ::v-deep .el-card__header {
        text-align: center;
        background-color: rgba(114, 124, 245, 1);
        color: #fff;
        line-height: 40px;
        font-size: 18px;
    }

    .el-form {
        padding: 0 20px;
    }
    .tc {
        text-align: center;
    }
}
</style>
